package com.keyinong.dingcan.view;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.keyinong.dingcan.R;
import com.keyinong.dingcan.Tools.HttpTools;
import com.keyinong.dingcan.Tools.Utility;
import com.keyinong.dingcan.adapter.HongbaoAdapter;
import com.keyinong.dingcan.entity.HongbaoEntity;
import com.loopj.android.http.JsonHttpResponseHandler;

public class HongbaoActivity extends Activity {
	private HongbaoAdapter hongbaoadapter;
	private ImageButton ib_back;
	private ListView lv_hongbao;
	private TextView tv_more;
	ArrayList<HongbaoEntity> hongbaolist;
	HttpTools httptools=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_hongbao);
		lv_hongbao=(ListView)findViewById(R.id.lv_hongbao);
		tv_more=(TextView)findViewById(R.id.tv_more);
		Hongbao();
		ib_back=(ImageButton)findViewById(R.id.ib_back);
		ib_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	private void Hongbao() {
		hongbaolist=new ArrayList<HongbaoEntity>();
		SharedPreferences sp = getSharedPreferences("spReg",
				Context.MODE_PRIVATE);
		String token = sp.getString("data", "");
//		Log.i("shi", "token---" + token);
		if (httptools == null) {
			httptools = new HttpTools(HongbaoActivity.this);
		}
		httptools.myCoupon(token, new JsonHongbao());
	}
	public class JsonHongbao extends JsonHttpResponseHandler {
		@Override
		public void onSuccess(int statusCode, Header[] headers,
				JSONArray response) {
			// TODO Auto-generated method stub
			super.onSuccess(statusCode, headers, response);
			try {
				for (int i = 0; i < response.length(); i++) {
					JSONObject data = response.getJSONObject(i);
					String id=data.getString("id");
					String more=data.getString("more");
					String off=data.getString("off");
					String starttime=data.getString("starttime");
					String endtime=data.getString("endtime");
					String note=data.getString("note");

					hongbaolist.add(new HongbaoEntity(id,off, more, starttime, endtime,note));
				}
				if (response.length()==0) {
					tv_more.setVisibility(View.VISIBLE);
				}else {
					tv_more.setVisibility(View.GONE);
				}
				hongbaoadapter=new HongbaoAdapter(HongbaoActivity.this, hongbaolist);
				lv_hongbao.setAdapter(hongbaoadapter);
				lv_hongbao.setDivider(null);
				Utility.setListViewHeightBasedOnChildren(lv_hongbao);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}
	

}
